import m3u8Json from "../assets/m3u8.js";
// m3u8接口列表
export default function (router, DBserve) {
  // 检测m3u8源数据
  DBserve({
    DBname: "m3u8",
    order: "count",
    option: {},
  }).then((data) => {
    // res.json(data);
    if (data.code == 200 && data.data == 0) {
      DBserve({
        DBname: "m3u8",
        order: "insert",
        option: m3u8Json,
      }).then((data) => {
        if (data.code == 200) console.log("插入m3u8源数据");
      });
    }
  });
  router.post("/m3u8", (req, res) => {
    DBserve({
      DBname: "m3u8",
      order: "find",
      option: {},
    }).then((data) => {
      res.json(data);
    });
  });
  // 修改
  router.post("/update/m3u8", (req, res) => {
    DBserve({
      DBname: "m3u8",
      order: "update",
      option: Object.assign(req.body),
    }).then((data) => {
      res.json(data);
    });
  });
  // 新增
  router.post("/add/m3u8", (req, res) => {
    // console.log(req.body);
    DBserve({
      DBname: "m3u8",
      order: "count",
      option: {
        $or: [{ label: req.body.label }, { url: req.body.url }],
      },
    }).then((data) => {
      if (data.data > 0)
        return res.json({ code: 201, message: "摄像头已存在" });
      DBserve({
        DBname: "m3u8",
        order: "insert",
        option: Object.assign(req.body),
      }).then((data) => {
        // console.log(data);
        res.json(data);
      });
    });
  });
  // 删除人员
  router.delete("/m3u8", (req, res) => {
    // console.log(req.body);
    DBserve({
      DBname: "m3u8",
      order: "remove",
      option: { _id: req.body._id },
    }).then((data) => {
      res.json(data);
    });
  });
}
